Imports CAPA_Entidad
Imports System.Data
Imports System.Data.SqlClient
Imports CAPA_Utilitarios
Public Class DocumentoClienteDetallePickeadorUbicacionDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function registrarErrorPicking(ByVal codCIA As String, ByVal codSUC As String, ByVal codTipo As String, ByVal codDCA As String, ByVal codDADP As String, ByVal codDADPU As String) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "UPDATE Documento_Almacen_Detalle_Pickeador_Ubicacion " & _
                                  "SET DADPU_ErrorPicking = '1' " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDO " & _
                                  "AND DCA_Codigo = @DCA AND DADP_Codigo = @DADP AND DADPU_Codigo = @DADPU " & _
                                  "AND DADPU_Activo = '1'"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDO", codTipo)
                .Parameters.AddWithValue("@DCA", codDCA)
                .Parameters.AddWithValue("@DADP", codDADP)
                .Parameters.AddWithValue("@DADPU", codDADPU)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function desactivarRegistrosDADPU(ByVal codCIA As String, ByVal codSUC As String, ByVal codTipo As String, ByVal codDCA As String, ByVal codDAP As String) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "UPDATE dadpu " & _
                                  "SET DADPU_Activo = '0' " & _
                                  "FROM Documento_Almacen_Detalle_Pickeador_Ubicacion dadpu " & _
                                  "INNER JOIN Documento_Almacen_Detalle_Pickeador dadp " & _
                                  "ON dadp.CIA = dadpu.CIA AND dadp.SUC_Codigo = dadpu.SUC_Codigo " & _
                                  "AND dadp.TDOC_Codigo = dadpu.TDOC_Codigo AND dadp.DCA_Codigo = dadpu.DCA_Codigo " & _
                                  "AND dadp.DADP_Codigo = dadpu.DADP_Codigo AND dadp.DAP_Codigo = @DAP AND dadp.DADP_Activo = '1' " & _
                                  "WHERE dadpu.CIA = @CIA AND dadpu.SUC_Codigo = @SUC AND dadpu.TDOC_Codigo = @TDO " & _
                                  "AND dadpu.DCA_Codigo = @DCA AND dadpu.DADPU_FechaInicio IS NULL AND dadpu.DADPU_Activo = '1'"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@DAP", codDAP)
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDO", codTipo)
                .Parameters.AddWithValue("@DCA", codDCA)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function obtenerUltimoCodigoDADPU(ByVal tipoDOC As String) As Integer
        Dim codigo As Integer = 0
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "SELECT COUNT(*) + 1 AS Contar " & _
                                  "FROM Documento_Almacen_Detalle_Pickeador_Ubicacion " & _
                                  "WHERE TDOC_Codigo = '" & tipoDOC & "'"

            Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, con)
            Dim ds As New DataSet
            adapter.Fill(ds)

            codigo = CInt(ds.Tables(0).Rows(0)("Contar").ToString.Trim)
        Catch ex As Exception
            codigo = -1
        Finally
            con.Close()
        End Try

        Return codigo
    End Function

End Class
